package com.zhaoyh.controller;

import com.alibaba.druid.util.StringUtils;
import com.zhaoyh.bean.Userinfo;
import com.zhaoyh.service.UserinfoService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletResponse;

/**
 * @author 祥霸
 * @date 2019/2/20
 */
@Controller
@RequestMapping("/userinfo")
public class UserinfoController extends BaseController{

    @Autowired
    UserinfoService userinfoService;

    @RequestMapping(value = "/register", method = RequestMethod.GET)
    public void register(@RequestParam(value = "username")String username, @RequestParam(value = "pwd")String pwd, HttpServletResponse responser) {

        try {
            System.out.println(username);
            System.out.println(pwd);
            Userinfo userinfo = userinfoService.getUserinfo(username);

            if (userinfo != null) {
                renderData(responser, "用户名已存在");
                return;
            }
            userinfoService.insertUserinfo(username, pwd);
            renderData(responser, "success");
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

    @RequestMapping(value = "/loging", method = RequestMethod.GET)
    public void loging(@RequestParam(value = "username")String username, @RequestParam(value = "pwd")String pwd, HttpServletResponse responser) {
        try {
            Userinfo userinfo = userinfoService.getUserinfo(username);
            if(userinfo == null || !userinfo.getPwd().equals(pwd)) {
                renderData(responser, "用户名或密码错误");
                return;
            }
            renderData(responser, "success|" + userinfo.getUuid());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
